<template>
  <div class="bottom-bar">
    <div class="check-content">
      <check-button
        class="check-button"
        :isChecked="isSelectAll"
        @click.native="checkedClick"
      />
      <span>全选</span>
    </div>
    <div class="totalprice">合计：{{ totalPrice }}</div>
    <div class="jiesuan" @click="calcClick">去计算({{ checkLength }})</div>
  </div>
</template>

<script>
import CheckButton from "components/content/checkButton/CheckButton.vue";
import { mapGetters } from "vuex";
export default {
  components: { CheckButton },
  methods: {
    checkedClick() {
      //   console.log('sss');
      if (this.isSelectAll) {
        //全部选中判断
        this.cartList.forEach((item) => (item.checked = false));
      } else {
        //有部分不选中或者全都不选中的时候 点击全选 全部变成选中
        this.cartList.forEach((item) => (item.checked = true));
      }
    },
    calcClick() {
      if (!this.isSelectAll) {
        this.$toast.show("请选择购买的商品", 2000);
        return;
      }
      this.$emit("qrCode");
    },
  },
  computed: {
    ...mapGetters(["cartList"]),
    totalPrice() {
      return (
        "￥" +
        this.cartList
          .filter((item) => {
            return item.checked;
          })
          .reduce((pre, item) => {
            return pre + item.price * item.count;
          }, 0)
          .toFixed(2)
      );
    },
    checkLength() {
      return this.cartList.filter((item) => item.checked).length;
    },
    isSelectAll() {
      //判断里面的有一个不选中 全选按钮就不选中 进行取反

      //1.使用filter
      // if(this.cartList.length === 0) return false
      //   return !(this.cartList.filter(item => !item.checked).length)
      //如果购物车里面的没有东西也就是数组的长度为0的时候直接返回false不选中
      //2.使用find
      if (this.cartList.length === 0) return false;
      return !this.cartList.find((item) => !item.checked);
      //3.普通遍历
      // if(this.cartList.length === 0) return false
      // for (let item of this.cartList){
      //     if(!item.checked){
      //         return false
      //     }
      // }
      // return true
    },
  },
};
</script>

<style scoped>
.bottom-bar {
  display: flex;
  height: 40px;
  background-color: #eee;
  position: relative;
  line-height: 40px;
  font-size: 14px;
}
.check-content {
  display: flex;
  align-items: center;
  margin-left: 10px;
  width: 60px;
}
.check-button {
  width: 20px;
  height: 20px;
  line-height: 20px;
  margin-right: 5px;
}
.totalprice {
  margin-left: 20px;
  flex: 1;
}
.jiesuan {
  width: 90px;
  background-color: red;
  color: #fff;
  text-align: center;
}
</style>